/**
 * json配置举例，更多配置请看 IConfigSchema
 * @author luguoxiang
 * @date 2022/7/5
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
import { IConfigSchema, LgBizTree, LgBizTreeProps } from '@/soar';
import api, { basePath } from './api';
import { basePath as menuBasePath } from '../menu/api';
import { basePath as resourceBasePath } from '../dataScope/resourceApi';
import { basePath as ruleBasePath } from '../dataScope/ruleApi';
import { MenuComponent } from '@/types/interface';

const fieldList: IConfigSchema<any>[] = [
  {
    field: 'name',
    label: '名称',
    dataType: 'string',
    form: {
      rules: true, // true方式配置校验，必填
    },
  },
  {
    field: 'type',
    label: '类型',
    dataType: 'dict',
    params: 'RoleTypeEnum',
    table: {
      width: 120,
      sorter: true,
    },
    form: {
      disabled: ({ isUpdate, data }) => {
        return isUpdate || !!data.parentId;
      },
      rules: true,
      formItemProps({ data }) {
        return {
          help: data.type === 1 ? '上级继承所有下级的权限' : data.type === 2 ? '子级继承所有上级的权限' : '',
        };
      },
    },
  },
  {
    field: 'parentId',
    label: '所属父级',
    dataType: 'string',
    table: false,
    search: false,
    form: {
      vif({ data }): boolean {
        return data.type !== 0;
      },
      component: LgBizTree,
      componentProps({ data }) {
        if (!data.type) {
          return {};
        }
        return {
          filters: [`id!=${data.id ?? 0}`, `type=${data.type}`],
          fields: 'id,name,parentId',
          data: (params: any) => {
            return api.query(params);
          },
          prepend: { value: '0', label: '根节点' },
        } as LgBizTreeProps;
      },
    },
  },
  {
    field: 'code',
    label: '编码',
    dataType: 'string',
    table: {
      sorter: true,
    },
    form: {
      disabled: ({ isUpdate }) => isUpdate,
      rules: true,
    },
  },
  {
    field: 'alias',
    label: '别名(简称)',
    dataType: 'string',
  },
  {
    field: 'remark',
    label: '备注',
    dataType: 'string',
    form: {
      component: 't-textarea',
    },
  },
];

export default fieldList;

/**
 * 组件配置（用于生成菜单数据）
 */
export const COMPONENTS: MenuComponent[] = [
  {
    title: '角色管理',
    component: () => import('./List.vue'),
    sort: 40,
    keepalive: true,
    icon: 'usergroup',
    permits: [`GET${basePath}/query`, `GET${menuBasePath}/query`],
    elements: [
      { title: '新增', code: 'create', permits: [`POST${basePath}/create`] },
      { title: '更新', code: 'update', permits: [`POST${basePath}/update`, `GET${basePath}/detail`] },
      { title: '删除', code: 'remove', permits: [`POST${basePath}/remove`] },
      { title: '详情', code: 'detail', permits: [`GET${basePath}/detail`] },
      { title: '导入', code: 'import', permits: [`POST${basePath}/import`] },
      { title: '导出', code: 'export', permits: [`GET${basePath}/export`] },
      { title: '清除缓存', code: 'clear-cache', permits: [`POST${basePath}/clear-cache`] },
      { title: '功能权限', code: 'menu', permits: [`POST${basePath}/set-menu-ids`, `GET${basePath}/get-menu-ids`] },
      {
        title: '数据权限',
        code: 'datascope',
        permits: [
          `POST${basePath}/set-data-scope`,
          `GET${basePath}/get-data-scope`,
          `GET${resourceBasePath}/query`,
          `GET${ruleBasePath}/query`,
        ],
      },
    ],
  },
];
